/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import control.Manager;
import control.Start;
import framework.dal.ItemDB;
import framework.groups.Item;
import javax.swing.JOptionPane;
import static javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE;

/**
 *
 * @author Guilherme
 */
public class NoteUI extends javax.swing.JInternalFrame
{
    
    private boolean dirty;
    private Item note;

    /**
     * Creates new form NoteUI
     */
    public NoteUI()
    {
        initComponents();
        this.dirty = false;
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        this.note = new Item();
//        ((BasicInternalFrameUI)this.getUI()).setNorthPane(null); // remove painel superior
    }
    
    public NoteUI(Item n)
    {
        initComponents();
        this.note = n;
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        this.jEditorPane1.setText(this.note.getDesc());
        dirty = false;
//        ((BasicInternalFrameUI)this.getUI()).setNorthPane(null); // remove painel superior
    }
    
    public String getDescription()
    {
        return this.jEditorPane1.getText();
    }
    
    public boolean salve()
    {
        if (this.note.getIdentifier() == null)
        {
            return this.add();
        }
        else
        {
            return this.update();
        }
    }
    
    public boolean update()
    {
        this.note.setDesc(this.jEditorPane1.getText());
        return Manager.STRATEGY.alterarItem(this.note);
    }
    
    public boolean add()
    {
        this.note.setDesc(this.jEditorPane1.getText());
        return Manager.STRATEGY.addItem(this.note);
    }

    public Item getNote()
    {
        this.note.setDesc(this.jEditorPane1.getText());
        return note;
    }
    
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        jScrollPane1 = new javax.swing.JScrollPane();
        jEditorPane1 = new javax.swing.JEditorPane();

        setBackground(new java.awt.Color(248, 247, 182));
        setBorder(null);
        setClosable(true);
        setToolTipText("");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/ui/images/note.png"))); // NOI18N
        setMinimumSize(new java.awt.Dimension(200, 200));
        setName(""); // NOI18N
        setPreferredSize(new java.awt.Dimension(200, 200));
        addInternalFrameListener(new javax.swing.event.InternalFrameListener()
        {
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt)
            {
                formInternalFrameIconified(evt);
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt)
            {
                formInternalFrameClosing(evt);
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt)
            {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt)
            {
                close(evt);
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt)
            {
            }
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt)
            {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt)
            {
            }
        });

        jEditorPane1.setBackground(new java.awt.Color(248, 247, 182));
        jEditorPane1.setFont(new java.awt.Font("Segoe Print", 0, 14)); // NOI18N
        jScrollPane1.setViewportView(jEditorPane1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void close(javax.swing.event.InternalFrameEvent evt)//GEN-FIRST:event_close
    {//GEN-HEADEREND:event_close
        Manager.STRATEGY.removeItem(note);
    }//GEN-LAST:event_close
        
    private void formInternalFrameIconified(javax.swing.event.InternalFrameEvent evt)//GEN-FIRST:event_formInternalFrameIconified
    {//GEN-HEADEREND:event_formInternalFrameIconified
        
    }//GEN-LAST:event_formInternalFrameIconified
    
    private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt)//GEN-FIRST:event_formInternalFrameClosing
    {//GEN-HEADEREND:event_formInternalFrameClosing
        int res = JOptionPane.showConfirmDialog(null, "Excluir a anotação?");
        if (res == 0)
        {
            if((this.note.getIdentifier() == null) || Manager.STRATEGY.removeItem(this.note))
            {
                this.dispose();
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Ops! Falha na remoção da anotação! :(\nVerifique a conexão com o banco de dados");
            }
        } else
        {
            return;
        }
    }//GEN-LAST:event_formInternalFrameClosing
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JEditorPane jEditorPane1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
